#pragma once

#include "TextureSynthesizer.h"

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


namespace TextureSynthesizer {

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  buttonUniformTex;

	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::PictureBox^  pictureBox1;
	private: System::Windows::Forms::Button^  button4;
	private: System::Windows::Forms::Button^  buttonExtractGrid;







	private: System::Windows::Forms::PropertyGrid^  propertyGrid1;
	private: System::Windows::Forms::Panel^  panel1;

	private: System::Windows::Forms::Panel^  panel3;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->buttonUniformTex = (gcnew System::Windows::Forms::Button());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->buttonExtractGrid = (gcnew System::Windows::Forms::Button());
			this->propertyGrid1 = (gcnew System::Windows::Forms::PropertyGrid());
			this->panel1 = (gcnew System::Windows::Forms::Panel());
			this->panel3 = (gcnew System::Windows::Forms::Panel());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
			this->panel1->SuspendLayout();
			this->panel3->SuspendLayout();
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(12, 12);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(92, 29);
			this->button1->TabIndex = 0;
			this->button1->Text = L"Load";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// buttonUniformTex
			// 
			this->buttonUniformTex->Enabled = false;
			this->buttonUniformTex->Location = System::Drawing::Point(12, 123);
			this->buttonUniformTex->Name = L"buttonUniformTex";
			this->buttonUniformTex->Size = System::Drawing::Size(92, 31);
			this->buttonUniformTex->TabIndex = 2;
			this->buttonUniformTex->Text = L"Uniform Texture";
			this->buttonUniformTex->UseVisualStyleBackColor = true;
			this->buttonUniformTex->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// button3
			// 
			this->button3->Enabled = false;
			this->button3->Location = System::Drawing::Point(12, 160);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(92, 32);
			this->button3->TabIndex = 3;
			this->button3->Text = L"Model Texture";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// pictureBox1
			// 
			this->pictureBox1->Dock = System::Windows::Forms::DockStyle::Fill;
			this->pictureBox1->Location = System::Drawing::Point(120, 0);
			this->pictureBox1->Name = L"pictureBox1";
			this->pictureBox1->Padding = System::Windows::Forms::Padding(3);
			this->pictureBox1->Size = System::Drawing::Size(286, 348);
			this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::AutoSize;
			this->pictureBox1->TabIndex = 3;
			this->pictureBox1->TabStop = false;
			// 
			// button4
			// 
			this->button4->Enabled = false;
			this->button4->Location = System::Drawing::Point(12, 47);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(92, 29);
			this->button4->TabIndex = 1;
			this->button4->Text = L"Extract Textons";
			this->button4->UseVisualStyleBackColor = true;
			this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
			// 
			// buttonExtractGrid
			// 
			this->buttonExtractGrid->Enabled = false;
			this->buttonExtractGrid->Location = System::Drawing::Point(12, 82);
			this->buttonExtractGrid->Name = L"buttonExtractGrid";
			this->buttonExtractGrid->Size = System::Drawing::Size(92, 35);
			this->buttonExtractGrid->TabIndex = 1;
			this->buttonExtractGrid->Text = L"Extract w/Grid";
			this->buttonExtractGrid->UseVisualStyleBackColor = true;
			this->buttonExtractGrid->Click += gcnew System::EventHandler(this, &Form1::button5_Click);
			// 
			// propertyGrid1
			// 
			this->propertyGrid1->Dock = System::Windows::Forms::DockStyle::Fill;
			this->propertyGrid1->Location = System::Drawing::Point(0, 0);
			this->propertyGrid1->Name = L"propertyGrid1";
			this->propertyGrid1->Size = System::Drawing::Size(150, 348);
			this->propertyGrid1->TabIndex = 6;
			// 
			// panel1
			// 
			this->panel1->Controls->Add(this->propertyGrid1);
			this->panel1->Dock = System::Windows::Forms::DockStyle::Right;
			this->panel1->Location = System::Drawing::Point(406, 0);
			this->panel1->MaximumSize = System::Drawing::Size(150, 0);
			this->panel1->Name = L"panel1";
			this->panel1->Size = System::Drawing::Size(150, 348);
			this->panel1->TabIndex = 7;
			// 
			// panel3
			// 
			this->panel3->Controls->Add(this->buttonExtractGrid);
			this->panel3->Controls->Add(this->button4);
			this->panel3->Controls->Add(this->button3);
			this->panel3->Controls->Add(this->buttonUniformTex);
			this->panel3->Controls->Add(this->button1);
			this->panel3->Dock = System::Windows::Forms::DockStyle::Left;
			this->panel3->Location = System::Drawing::Point(0, 0);
			this->panel3->MaximumSize = System::Drawing::Size(120, 0);
			this->panel3->MinimumSize = System::Drawing::Size(120, 0);
			this->panel3->Name = L"panel3";
			this->panel3->Size = System::Drawing::Size(120, 348);
			this->panel3->TabIndex = 9;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(556, 348);
			this->Controls->Add(this->pictureBox1);
			this->Controls->Add(this->panel3);
			this->Controls->Add(this->panel1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
			this->panel1->ResumeLayout(false);
			this->panel3->ResumeLayout(false);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
		 String^ m_picFilename;
		 Image^ m_srcImg;
		 TextureSynthesizer::TexSynthProps^ m_props;
// Load Texture button
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 		 
		 {			
			 OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
			if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
			{
				m_picFilename = openFileDialog1->FileName;

				if(!m_props) {
					m_props = gcnew TextureSynthesizer::TexSynthProps();
					this->propertyGrid1->SelectedObject = m_props;
				}
				m_props->Filename = m_picFilename;

				m_srcImg = Image::FromFile(m_picFilename);
				pictureBox1->Image = m_srcImg;

				buttonExtractGrid->Enabled = true;
				buttonUniformTex->Enabled = false;
				button3->Enabled = false;
			}
		 }
private: System::Void cleanDir() {
					  			 //erase all files in "../Output/"
			 if(!System::IO::Directory::Exists("../Output")) {
				 System::IO::Directory::CreateDirectory("../Output");
			 } else {
				 cli::array<System::String^,1>^ a = System::IO::Directory::GetFiles("../Output/");
				 for(int i=0;i<a->Length;i++) {
					 System::IO::File::Delete(a[i]);
				 }
			 }
		}
// Extract Textons button
private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) 		 
		 {			 
			cleanDir();
			 Threading::ParameterizedThreadStart^ start = 				 
				 gcnew Threading::ParameterizedThreadStart(				
				 TextureSynthesizer::ExtractTextons);			 
			 Threading::Thread^ t = gcnew Threading::Thread(start);			 
			 t->Start(m_picFilename);		 
		 }		 
// create model texture button
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) 		 
		 {			 
			 Threading::ParameterizedThreadStart^ start = 				 
				 gcnew Threading::ParameterizedThreadStart(				
				 TextureSynthesizer::CreateNewTexture);			 
			 Threading::Thread^ t = gcnew Threading::Thread(start);			 
			 t->Start(m_props);
		 }
//create uniform texture button
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e)
		 {
			 Threading::ParameterizedThreadStart^ start = 				 
				 gcnew Threading::ParameterizedThreadStart(				
				 TextureSynthesizer::CreateUniformTexture);			 
			 Threading::Thread^ t = gcnew Threading::Thread(start);			 
			 t->Start(m_props);		 
		 }
private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) {
			cleanDir();
			 Threading::ParameterizedThreadStart^ start = 				 
				 gcnew Threading::ParameterizedThreadStart(				
				 TextureSynthesizer::ExtractTextonsUsingGrid);			 
			 Threading::Thread^ t = gcnew Threading::Thread(start);			 
			 t->Start(m_props);	

			 buttonUniformTex->Enabled = true;
			 button3->Enabled = true;
		 }

};
}
